package com.syni.mdd.yhd.common.all.service;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.sjb.common.datatreasure.DxshApiIndustryRestService;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsbusinessTypeDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessLabel;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessType;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsBusinessTypeService extends BaseService<BmsBusinessType, Integer> {
    @Autowired
    BmsbusinessTypeDao bmsbusinessTypeDao;

    @Autowired
    BmsBusinessLabelService bmsBusinessLabelService;

    @Autowired
    DxshApiIndustryRestService dxshApiIndustryRestService;

    @Override
    public BmsbusinessTypeDao getBaseDao() {
        return bmsbusinessTypeDao;
    }

    public BmsBusinessType findByTypeName(String typeName){
        BmsBusinessType bmsBusinessType = bmsbusinessTypeDao.findByTypeNameAndIsDelete(typeName, Constants.COMMON_FLAG_FALSE_INT);
        return bmsBusinessType;
    }

    public Map<Integer,BmsBusinessType> getBmsBusinessTypeMap(){
        Map<Integer,BmsBusinessType> bmsBusinessTypeMap = new HashMap<>();
        List<BmsBusinessType> bmsBusinessTypes = bmsbusinessTypeDao.findByIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        if(bmsBusinessTypes != null && bmsBusinessTypes.size() > 0){
            for(BmsBusinessType bmsBusinessType : bmsBusinessTypes){
                bmsBusinessTypeMap.put(bmsBusinessType.getId(),bmsBusinessType);
            }
        }
        return bmsBusinessTypeMap;
    }

    public Map<String, Object> findBusinessType(ParamsMap paramMap) {
        Integer userIdentify = Integer.parseInt(paramMap.get("userIdentify").toString());
        //获取该用户选择的行业
        RpcResponseVo rpcResponseVo = dxshApiIndustryRestService.getBusinessTypeByChooseIndustry(userIdentify);
        if(!rpcResponseVo.isSuccess()){
            return ResultMapHelper.success(new ArrayList<>(), ResultMsg.MSG_GET_SUCCESS);
        }
        List<String> typeNames = (List<String>) rpcResponseVo.getData();
        Set<String> typeNameSet = new HashSet<>(typeNames);
        Integer pageNum = Integer.parseInt(paramMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramMap.get("page_size").toString());
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.DESC,"newTime");
        Map<String,Object> condition = new HashMap<>();
        condition.put("isDelete",Constants.COMMON_FLAG_FALSE_INT);
        if(typeNames.size() > 0){
            condition.put("typeName_IN",typeNameSet);
        }
        Page<BmsBusinessType> bmsBusinessTypes = bmsbusinessTypeDao.findAll(new SpecificationUtils(condition),pageable);
        return ResultMapHelper.success(bmsBusinessTypes.getContent(), ResultMsg.MSG_GET_SUCCESS);
    }

    public Map<String, Object> getBusinessTypeAndLabel(ParamsMap paramMap) {
        //查询有团购的店铺
        String addrCity = paramMap.getString("addrCity");
        String addrDistrict = paramMap.getString("addrDistrict");
        List<Map<String,Object>> typeIdAndLabel = this.selfFindHavingGroupBuyType(addrCity,addrDistrict);
        Set<Integer> typeIds = new HashSet<>();
        Set<Integer> labelIds = new HashSet<>();
        if(typeIdAndLabel != null && typeIdAndLabel.size() > 0){
            for(Map<String,Object> obj : typeIdAndLabel){
                typeIds.add(Integer.parseInt(obj.get("bms_type_id").toString()));
                labelIds.add(Integer.parseInt(obj.get("id").toString()));
            }
        }
        List<BmsBusinessType> resultTypes = new LinkedList<>();
        Map<Integer,BmsBusinessType> bmsBusinessTypeMap = new HashMap<>();
        if(typeIds.size() > 0) {
            //查出所有类型
            resultTypes = bmsbusinessTypeDao.findByIdInAndIsDelete(typeIds, Constants.COMMON_FLAG_FALSE_INT);
            if(resultTypes != null && resultTypes.size() > 0) {
                for(BmsBusinessType bmsBusinessType : resultTypes) {
                    bmsBusinessTypeMap.put(bmsBusinessType.getId(),bmsBusinessType);
                }
            }
        }
        if (labelIds.size() > 0 && resultTypes.size() > 0) {
            List<BmsBusinessLabel> labels = bmsBusinessLabelService.findByIdIn(labelIds);
            if(labels != null && labels.size() > 0) {
                for(BmsBusinessLabel bmsBusinessLabel : labels) {
                    if(bmsBusinessTypeMap.containsKey(bmsBusinessLabel.getTypeId())) {
                        bmsBusinessTypeMap.get(bmsBusinessLabel.getTypeId()).getBmsBusinessLabelList().add(bmsBusinessLabel);
                    }
                }
            }
        }
        return ResultMapHelper.success(resultTypes,ResultMsg.MSG_GET_SUCCESS);

    }

    public List<Map<String, Object>> selfFindHavingGroupBuyType(String addrCity,String addrDistinct){
        List<Map<String, Object>> typeIdsMap;
        if(StringUtils.isNotBlank(addrCity) && StringUtils.isNotBlank(addrDistinct)) {
            typeIdsMap = bmsbusinessTypeDao.selfFindHavingGroupBuyType(addrCity,addrDistinct);
        }else if(StringUtils.isNotBlank(addrCity) && StringUtils.isBlank(addrDistinct)){
            typeIdsMap = bmsbusinessTypeDao.selfFindHavingGroupBuyType(addrCity,addrDistinct);
        }else {
            typeIdsMap = bmsbusinessTypeDao.selfFindHavingGroupBuyType();
        }
        return typeIdsMap;
    }

    public void postHandleBusinessType(List<BmsBusiness> bmsBusinesses){
        Map<Integer,BmsBusinessType> bmsBusinessTypeMap = this.getBmsBusinessTypeMap();
        bmsBusinesses.forEach(bb -> {
            if(bmsBusinessTypeMap.containsKey(bb.getTagsId())){
                bb.setBmsBusinessType(bmsBusinessTypeMap.get(bb.getTagsId()));
            }
        });
    }

    public void postHandleBusinessType(BmsBusiness bmsBusiness) {
        List<BmsBusiness> bList = new ArrayList<>();
        bList.add(bmsBusiness);
        postHandleBusinessType(bList);
    }

    public Set<Integer> getBmsBusinessTypeByJsonName(String typeName) {
        List<String> typeNames = JSON.parseArray(typeName,String.class);
        Set<Integer> ids = new HashSet<>();
        List<BmsBusinessType> bmsBusinessTypes = bmsbusinessTypeDao.findAll();
        if(CollectionUtils.isNotEmpty(bmsBusinessTypes)){
            for(BmsBusinessType bmsBusinessType : bmsBusinessTypes){
                if(typeNames.contains(bmsBusinessType.getTypeName())){
                    ids.add(bmsBusinessType.getId());
                }
            }
        }
        //防止报错
        if(ids.size() == 0){
            ids.add(0);
//            BmsBusinessType bmsBusinessType = this.findByTypeName("美食");
//            if(bmsBusinessType != null){
//                ids.add(bmsBusinessType.getId());
//            }else {
//                ids.add(0);
//            }
        }
        return ids;
    }
}
